//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package events_subscribe

import (
	"fmt"
	"strings"

	"atomgit.com/openlinksaas/proto-gen-go.git/events/events_project"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_api"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_subscribe_api"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_recycle_api"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_watch_api"
)

func getChatBotStr(chatBotType uint32) string {
	if chatBotType == uint32(events_subscribe_api.CHAT_BOT_TYPE_CHAT_BOT_QYWX) {
		return "企业微信"
	} else if chatBotType == uint32(events_subscribe_api.CHAT_BOT_TYPE_CHAT_BOT_DING) {
		return "钉钉"
	} else if chatBotType == uint32(events_subscribe_api.CHAT_BOT_TYPE_CHAT_BOT_FS) {
		return "飞书"
	}
	return ""
}

func getWatchTargetStr(targetType uint32) string {
	if targetType == uint32(project_watch_api.WATCH_TARGET_TYPE_WATCH_TARGET_ENTRY) {
		return "内容"
	}
	if targetType == uint32(project_watch_api.WATCH_TARGET_TYPE_WATCH_TARGET_REQUIRE_MENT) {
		return "需求"
	}
	if targetType == uint32(project_watch_api.WATCH_TARGET_TYPE_WATCH_TARGET_TASK) {
		return "任务"
	}
	if targetType == uint32(project_watch_api.WATCH_TARGET_TYPE_WATCH_TARGET_BUG) {
		return "缺陷"
	}
	if targetType == uint32(project_watch_api.WATCH_TARGET_TYPE_WATCH_TARGET_TEST_CASE) {
		return "测试用例"
	}
	return ""
}

func getRecycleItemTypeStr(recycleItemType uint32) string {
	if recycleItemType == uint32(project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_IDEA) {
		return "知识点"
	} else if recycleItemType == uint32(project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_REQUIREMENT) {
		return "项目需求"
	} else if recycleItemType == uint32(project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_TASK) {
		return "任务"
	} else if recycleItemType == uint32(project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_BUG) {
		return "缺陷"
	} else if recycleItemType == uint32(project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_TESTCASE) {
		return "测试用例"
	} else if recycleItemType == uint32(project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_BULLETIN) {
		return "项目公告"
	} else if recycleItemType == uint32(project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_SPRIT) {
		return "工作计划"
	} else if recycleItemType == uint32(project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_DOC) {
		return "项目文档"
	} else if recycleItemType == uint32(project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_PAGES) {
		return "静态网页"
	} else if recycleItemType == uint32(project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_BOARD) {
		return "信息面板"
	} else if recycleItemType == uint32(project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_FILE) {
		return "文件"
	} else if recycleItemType == uint32(project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_API_COLL) {
		return "接口集合"
	}
	return ""
}

func processProjectEvent(event *events_api.Event, cfgItemList []*CfgItem) (bool, error) {
	var err error
	var msg string

	{
		createProjectEvent := &events_project.CreateProjectEvent{}
		if event.EventData.MessageIs(createProjectEvent) {
			err = event.EventData.UnmarshalTo(createProjectEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 创建项目 %s。", event.UserDisplayName, event.ProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.CreateProject {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		updateProjectEvent := &events_project.UpdateProjectEvent{}
		if event.EventData.MessageIs(updateProjectEvent) {
			err = event.EventData.UnmarshalTo(updateProjectEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 更新项目 %s(原项目名 %s)。", event.UserDisplayName, updateProjectEvent.NewProjectName, event.ProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.UpdateProject {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		openProjectEvent := &events_project.OpenProjectEvent{}
		if event.EventData.MessageIs(openProjectEvent) {
			err = event.EventData.UnmarshalTo(openProjectEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 激活项目 %s。", event.UserDisplayName, event.ProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.OpenProject {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		closeProjectEvent := &events_project.CloseProjectEvent{}
		if event.EventData.MessageIs(closeProjectEvent) {
			err = event.EventData.UnmarshalTo(closeProjectEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 关闭项目 %s。", event.UserDisplayName, event.ProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.CloseProject {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		removeProjectEvent := &events_project.RemoveProjectEvent{}
		if event.EventData.MessageIs(removeProjectEvent) {
			err = event.EventData.UnmarshalTo(removeProjectEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 删除项目 %s。", event.UserDisplayName, event.ProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.RemoveProject {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		genInviteEvent := &events_project.GenInviteEvent{}
		if event.EventData.MessageIs(genInviteEvent) {
			err = event.EventData.UnmarshalTo(genInviteEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 发送项目邀请。", event.UserDisplayName, event.ProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.GenInvite {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		joinProjectEvent := &events_project.JoinProjectEvent{}
		if event.EventData.MessageIs(joinProjectEvent) {
			err = event.EventData.UnmarshalTo(joinProjectEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 加入项目 %s。", event.UserDisplayName, event.ProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.JoinProject {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		leaveProjectEvent := &events_project.LeaveProjectEvent{}
		if event.EventData.MessageIs(leaveProjectEvent) {
			err = event.EventData.UnmarshalTo(leaveProjectEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 离开项目 %s。", event.UserDisplayName, event.ProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.LeaveProject {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		createRoleEvent := &events_project.CreateRoleEvent{}
		if event.EventData.MessageIs(createRoleEvent) {
			err = event.EventData.UnmarshalTo(createRoleEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 创建角色 %s。", event.UserDisplayName, event.ProjectName, createRoleEvent.RoleName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.CreateRole {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		updateRoleEvent := &events_project.UpdateRoleEvent{}
		if event.EventData.MessageIs(updateRoleEvent) {
			err = event.EventData.UnmarshalTo(updateRoleEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 更新角色 %s(原标题 %s)。", event.UserDisplayName, event.ProjectName,
				updateRoleEvent.NewRoleName, updateRoleEvent.OldRoleName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.UpdateRole {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		removeRoleEvent := &events_project.RemoveRoleEvent{}
		if event.EventData.MessageIs(removeRoleEvent) {
			err = event.EventData.UnmarshalTo(removeRoleEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 删除角色 %s。", event.UserDisplayName, event.ProjectName, removeRoleEvent.RoleName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.RemoveRole {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		updateProjectMemberEvent := &events_project.UpdateProjectMemberEvent{}
		if event.EventData.MessageIs(updateProjectMemberEvent) {
			err = event.EventData.UnmarshalTo(updateProjectMemberEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 更新项目成员 %s。", event.UserDisplayName, event.ProjectName, updateProjectMemberEvent.NewMemberDisplayName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.UpdateProjectMember {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		removeProjectMemberEvent := &events_project.RemoveProjectMemberEvent{}
		if event.EventData.MessageIs(removeProjectMemberEvent) {
			err = event.EventData.UnmarshalTo(removeProjectMemberEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 删除项目成员 %s。", event.UserDisplayName, event.ProjectName, removeProjectMemberEvent.MemberDisplayName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.RemoveProjectMember {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		setProjectMemberRoleEvent := &events_project.SetProjectMemberRoleEvent{}
		if event.EventData.MessageIs(setProjectMemberRoleEvent) {
			err = event.EventData.UnmarshalTo(setProjectMemberRoleEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 设置 项目成员 %s 角色 %s。", event.UserDisplayName, event.ProjectName,
				setProjectMemberRoleEvent.MemberDisplayName, setProjectMemberRoleEvent.RoleName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.SetProjectMemberRole {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		changeOwnerEvent := &events_project.ChangeOwnerEvent{}
		if event.EventData.MessageIs(changeOwnerEvent) {
			err = event.EventData.UnmarshalTo(changeOwnerEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 转移 超级管理员 给 %s。", event.UserDisplayName, event.ProjectName, changeOwnerEvent.MemberDisplayName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.ChangeOwner {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		createEventSubscribeEvent := &events_project.CreateEventSubscribeEvent{}
		if event.EventData.MessageIs(createEventSubscribeEvent) {
			err = event.EventData.UnmarshalTo(createEventSubscribeEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 增加事件订阅 %s %s。", event.UserDisplayName, event.ProjectName,
				getChatBotStr(createEventSubscribeEvent.ChatBotType), createEventSubscribeEvent.ChatBotName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.CreateSubscribe {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		updateEventSubscribeEvent := &events_project.UpdateEventSubscribeEvent{}
		if event.EventData.MessageIs(updateEventSubscribeEvent) {
			err = event.EventData.UnmarshalTo(updateEventSubscribeEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 更新事件订阅 %s %s(原标题 %s)。", event.UserDisplayName, event.ProjectName,
				getChatBotStr(updateEventSubscribeEvent.ChatBotType), updateEventSubscribeEvent.NewChatBotName, updateEventSubscribeEvent.OldChatBotName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.UpdateSubscribe {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		removeEventSubscribeEvent := &events_project.RemoveEventSubscribeEvent{}
		if event.EventData.MessageIs(removeEventSubscribeEvent) {
			err = event.EventData.UnmarshalTo(removeEventSubscribeEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 删除事件订阅 %s %s。", event.UserDisplayName, event.ProjectName,
				getChatBotStr(removeEventSubscribeEvent.ChatBotType), removeEventSubscribeEvent.ChatBotName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.RemoveSubscribe {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		setAlarmConfigEvent := &events_project.SetAlarmConfigEvent{}
		if event.EventData.MessageIs(setAlarmConfigEvent) {
			err = event.EventData.UnmarshalTo(setAlarmConfigEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 更新预警配置 。", event.UserDisplayName, event.ProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.SetAlarmConfig {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		customEvent := &events_project.CustomEvent{}
		if event.EventData.MessageIs(customEvent) {
			err = event.EventData.UnmarshalTo(customEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s %s:%s", event.UserDisplayName, event.ProjectName, customEvent.EventType, customEvent.EventContent)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.CustomEvent {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		watchEvent := &events_project.WatchEvent{}
		if event.EventData.MessageIs(watchEvent) {
			err = event.EventData.UnmarshalTo(watchEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 关注 %s %s", event.UserDisplayName, event.ProjectName, getWatchTargetStr(watchEvent.TargetType), watchEvent.TargetTitle)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.Watch {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		unwatchEvent := &events_project.UnwatchEvent{}
		if event.EventData.MessageIs(unwatchEvent) {
			err = event.EventData.UnmarshalTo(unwatchEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 取消关注 %s %s", event.UserDisplayName, event.ProjectName, getWatchTargetStr(unwatchEvent.TargetType), unwatchEvent.TargetTitle)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.Unwatch {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		recoverFromRecycleEvent := &events_project.RecoverFromRecycleEvent{}
		if event.EventData.MessageIs(recoverFromRecycleEvent) {
			err = event.EventData.UnmarshalTo(recoverFromRecycleEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 从回收站恢复 %s %s", event.UserDisplayName, event.ProjectName, getRecycleItemTypeStr(recoverFromRecycleEvent.RecycleItemType), recoverFromRecycleEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.RecoverFromRecycle {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		removeFromRecycleEvent := &events_project.RemoveFromRecycleEvent{}
		if event.EventData.MessageIs(removeFromRecycleEvent) {
			err = event.EventData.UnmarshalTo(removeFromRecycleEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 从回收站删除 %s %s", event.UserDisplayName, event.ProjectName, getRecycleItemTypeStr(removeFromRecycleEvent.RecycleItemType), removeFromRecycleEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.RemoveFromRecycle {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		clearFromRecycleEvent := &events_project.ClearFromRecycleEvent{}
		if event.EventData.MessageIs(clearFromRecycleEvent) {
			err = event.EventData.UnmarshalTo(clearFromRecycleEvent)
			if err != nil {
				return false, err
			}
			typeNameList := []string{}
			for _, itemType := range clearFromRecycleEvent.RecycleItemTypeList {
				typeNameList = append(typeNameList, getRecycleItemTypeStr(itemType))
			}
			msg = fmt.Sprintf("%s 在项目 %s 从回收站清空 类型 %s ", event.UserDisplayName, event.ProjectName, strings.Join(typeNameList, ","))
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ProjectEvCfg.ClearFromRecycle {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	return false, nil
}
